/**
 * Created by zhoulin on 2019/4/3.
 */

var app = new Vue({
    el: '#app',
    data: {
        permissions:['department:update'],
        type:-1,
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200)

            return ;
        }
        //parentId paternalId
        that.type = aGET.type;
        $("#Form input[name='sort']").val(aGET.sort);
        if(1 == that.type){
            $("#Form input[name='id']").val(aGET.schoolId);
            that.getInfoById(aGET.schoolId);
        }
        if(2 == that.type){
            $("#Form input[name='id']").val(aGET.id);
            that.getInfoById(aGET.id);
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        getInfoById:function (id) {
            var that = this;
            var url = _HOST+_API.business_manage_sysDepartment_getInfoById;
            if(1 == that.type){
                url = _HOST+_API.business_manage_school_getInfoById;
            }
            Get(that,url+id)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response)
                    if(response){
                        if(response.success){
                            var list = response.data
                            for (var item in list) {
                                if(1 == that.type){
                                    if (item == 'description') {
                                        editor.txt.html(list[item]);
                                    }
                                    if (item == 'isGeneral') {
                                        $("#Form select[name='isGeneral']").val(list[item]);
                                    }
                                }

                                $("#Form input[name='" + item + "']").val(list[item]);
                            }

                            $("#Form").data('bootstrapValidator').destroy();
                            $('#Form').data('bootstrapValidator', null);
                            Validator();
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
        //
        update:function () {
            var that = this;
            $("#Form").data('bootstrapValidator').destroy();
            $('#Form').data('bootstrapValidator', null);
            Validator();
            var isForm = true;
            $("#desc").val(editor.txt.html());
            //var param = formToJson($("#Form").serialize());
            var param = {};
            if(that.type == 1){
                $("#Form").find('input,textarea,select').each(function(){
                    if(empty($(this).val())){
                        layer.msg($(this).attr("placeholder"), {time: 1500, icon:5});
                        $(this).focus()
                        isForm = false;
                        return false;
                    }
                    if($(this).attr("name") != "sort" && $(this).attr("name") != "type"){
                        param[$(this).attr('name')] = $(this).val();
                    }

                });
            }else if (that.type == 2){
                param = formToJson($("#Form").serialize());
            }

            console.log(param)
            if(Boolean(isForm)){
                var url = _HOST+_API.business_manage_sysDepartment_update;
                if(1 == that.type){
                    url = _HOST+_API.business_manage_sysDepartment_updateSchool+aGET.id+"/"+$("#Form input[name='sort']").val();
                }
                console.log(url)
                Put(that,url,param,true,that.permissions[0])
                    .then(function (response) {
                        // 响应成功回调
                        console.log(response)
                        if(response){
                            if(response.success){
                                layer.msg(response.msg, {time: 1500, icon:1});
                                setTimeout(function () {
                                    window.parent.location.reload(); //刷新父页面
                                    layer.closeAll();
                                }, 1500)
                            }else {
                                layer.msg(response.msg, {time: 1500, icon:5});
                            }
                        }
                        $("#Form").data('bootstrapValidator').destroy();
                        $('#Form').data('bootstrapValidator', null);
                        Validator();
                    })
            }
        },
    },
})

var E = window.wangEditor;
var editor = new E('#editor');
editor.customConfig.uploadImgServer = _HOST+_API.common_file_ossWe;
editor.customConfig.uploadFileName = 'file';
editor.customConfig.uploadImgHeaders = {
    'Authorization': sessionStorageUtils.getJson(storageKey.sessionStorageKey.token),
    'permCoding': '',
    'enctype': 'multipart/form-data'
};
editor.customConfig.zIndex = 100;
editor.create();

$('#Form').data('bootstrapValidator', null);
Validator();
function Validator() {
    $('#Form').bootstrapValidator({
        fields: {
            name: {
                validators: {
                    notEmpty: {
                        message: '名称不能为空'
                    },
                    stringLength: {
                        max: 50,
                        message: '名称长度最多50个字符'
                    }
                }
            },
            description: {
                validators: {
                    notEmpty: {
                        message: '描述不能为空'
                    },
                    stringLength: {
                        max: 500,
                        message: '描述长度最多500个字符'
                    }
                }
            },
            sort: {
                validators: {
                    notEmpty: {
                        message: '排序不能为空'
                    }
                }
            },
            addr: {
                validators: {
                    notEmpty: {
                        message: '学校地址不能为空'
                    },
                    stringLength: {
                        max: 500,
                        message: '学校地址长度最多500个字符'
                    }
                }
            },
            shortDesc: {
                validators: {
                    notEmpty: {
                        message: '简述不能为空'
                    },
                    stringLength: {
                        max: 500,
                        message: '简述长度最多500个字符'
                    }
                }
            },
            service: {
                validators: {
                    notEmpty: {
                        message: '主要负责人不能为空'
                    },
                    stringLength: {
                        max: 20,
                        message: '主要负责人长度最多20个字符'
                    }
                }
            },
            tel: {
                validators: {
                    notEmpty: {
                        message: '负责人电话不能为空'
                    },
                    phone: {
                        message: '请输入正确的电话号码',
                        country: 'CN'
                    },
                    stringLength: {
                        max: 20,
                        message: '负责人电话长度最多20个字符'
                    }
                }
            }
        }
    }).on('success.form.bv', function (e) {
        // Prevent form submission
        e.preventDefault();
        app.update();
    });
}